//
// Created by 32827 on 2024/5/19.
//
#include "user_ui.h"
#include "view/yzl_ui_view_demo.h"
#if YZ_SCREEN_DRIVER == YZ_SCREEN_DRIVER_I2C_1306
#include "yzl_lcd1306.h"
#endif
//TODO: 继续完善UI系统
YZF_BOOL Touch_temp=YZF_FALSE;

YZL_UI_ROOT YZL_UI_Root;
YZL_TOUCH_DATA yzlTouchData={0};
YZL_ACTIVE_DATA yzlActiveData={0};
//USE TO YZ GLOBAL SHEET
YZF_RESULT user_yz_thread_ui_main(void){
    if(!YZL_UI_IS_INIT){
        return YZF_NOTHING;
    }
    YZL_TopView(YZL_UI_Root).draw(&yzlActiveData);  //绘制

    /** Cpp View loop ex*/
    user_pp_ui_loop(&yzlTouchData);         //调用循环

    return YZF_NOTHING;
}
/**
 * For C part. you can use the code of below to add view.
 *  YZL_UI_Root.ViewTop++;
 *  yzl_ui_view_demo_create(&YZL_TopView(YZL_UI_Root));
 *  YZL_VIEW_DEMO_INIT demoInit={"/MMC/logo.565"};
 *
 *  yzlActiveData.handleView=&YZL_TopView(YZL_UI_Root);
 *  YZL_TopView(YZL_UI_Root).init(&demoInit, &yzlActiveData);
 *  YZL_TopView(YZL_UI_Root).wakeup(&yzlActiveData);
 * */
// 初始化 用于YZ 模拟线程
YZF_RESULT user_yz_thread_ui_main_init(void){
    /** init Root */
    YZL_UI_Root.ViewTop=-1;
    /** init touch data */
    yzlTouchData.touchSize=0;
    /** init active data */
    yzlActiveData.root=&YZL_UI_Root;
    yzlActiveData.touch_data=&yzlTouchData;
    yzlActiveData.Event=YZL_UI_CTR_EVENT_NONE;
    yzlActiveData.Data=NULL_PTR;

    //C++ View init at this
    /** init CPP Part*/
    user_pp_ui_init();
    return YZF_OK;
}
//use to RTOS
void user_ui_main(void){
    while(!YZL_UI_IS_INIT){
        // vTaskDelay(1);
    }
    /** init Root */
    YZL_UI_Root.ViewTop=-1;
    /** init touch data */
    yzlTouchData.touchSize=0;
    /** init active data */
    yzlActiveData.root=&YZL_UI_Root;
    yzlActiveData.touch_data=&yzlTouchData;
    yzlActiveData.Event=YZL_UI_CTR_EVENT_NONE;
    yzlActiveData.Data=NULL_PTR;
//    /** init View */
//    YZL_UI_Root.ViewTop++;
//    yzl_ui_view_demo_create(&YZL_TopView(YZL_UI_Root));
//    YZL_VIEW_DEMO_INIT demoInit={"/MMC/logo.565"};
//
//    yzlActiveData.handleView=&YZL_TopView(YZL_UI_Root);
//    YZL_TopView(YZL_UI_Root).init(&demoInit, &yzlActiveData);
//    YZL_TopView(YZL_UI_Root).wakeup(&yzlActiveData);

    //C++ View init at this
    /** init CPP Part*/
    user_pp_ui_init();

    /** UI Loop*/
    while (1){
        /** 获取触摸数据 */
//        gt1151_scan(0);
//        yzlTouchData.touchSize=tp_dev.sta&0b00011111;
//        for(int i=0;i<yzlTouchData.touchSize;i++){
//            yzlTouchData.Point[i].x=tp_dev.y[i];
//            yzlTouchData.Point[i].y=480-tp_dev.x[i];
//        }

        YZL_TopView(YZL_UI_Root).draw(&yzlActiveData);

        /** 检测触摸状态 并更新View */
//        if(Touch_temp&&tp_dev.x[0]==0xFFFF){
//            Touch_temp=YZF_FALSE;
//            yzlActiveData.Event=YZL_UI_CTR_EVENT_TOUCH_UP;
//            yzlActiveData.handleView=&YZL_TopView(YZL_UI_Root);
//            YZL_TopView(YZL_UI_Root).act(&yzlActiveData);
//        }else if((!Touch_temp)&&(tp_dev.x[0]!=0xFFFF)){
//            Touch_temp=YZF_TRUE;
//            yzlActiveData.Event=YZL_UI_CTR_EVENT_TOUCH_DOWN;
//            yzlActiveData.handleView=&YZL_TopView(YZL_UI_Root);
//            YZL_TopView(YZL_UI_Root).act(&yzlActiveData);
//        }else if(Touch_temp){
//            yzlActiveData.Event=YZL_UI_CTR_EVENT_TOUCH_MOVE;
//            yzlActiveData.handleView=&YZL_TopView(YZL_UI_Root);
//            YZL_TopView(YZL_UI_Root).act(&yzlActiveData);
//        }

        /** Cpp View loop ex*/
        user_pp_ui_loop(&yzlTouchData);

        /** 释放占用 */
//        vTaskDelay(1);
    }
}